<?php if(!empty($hanghoabhlds) && !empty($chitieungaybhlds)) {?>
 <?php
 
   /* if(empty($nhanvien_id)) 
        echo $this->Html->link(__('Xuất Excel', true), array('action' => 
        'layketquadvxls',array(),$donvi_id,$ngaybdbhld,$ngayktbhld));
    else
        echo $this->Html->link(__('Xuất Excel', true), array('action' => 
        'layketquadvxls',$donvi_id,$ngaybdbhld,$ngayktbhld));*/
//	echo $form->create('Kqbanhang',array('url'=>'layketquadvxls/'.$donvi_id.'/'.$ngaybdbhld.'/'.$ngayktbhld));
	
			$tongdk = $hanghoabhlds;
			$tongkq = $hanghoabhlds;
?>			<table id ='tables'>
				<tr>
				<th rowspan=3>STT</th>
				<th rowspan=3>Trung Tâm</th>
				<th rowspan=3>Nhân Viên</th>
				<th rowspan=3>Đội</th>
				<th rowspan=3>Địa Điểm</th>
				<th rowspan=3>Ngày</th>
				<th rowspan=3>Thành viên tham gia</th>
				<th colspan=<?php echo count($hanghoabhlds)*3?>>Chỉ tiêu</th>
				<th rowspan=3>Ghi chú</th>
				
				</tr>
				<tr>
				<?php foreach ($hanghoabhlds as $keyhh =>$valhh):?>
				<th colspan=3><?php echo $valhh; $tongdk[$keyhh]=0; $tongkq[$keyhh]=0; ?></th>
				<?php endforeach;?>
				</tr>
				<tr>
				<?php foreach ($hanghoabhlds as $keyhh =>$valhh):?>
				<th>Chỉ Tiêu</th>
				<th>Kết quả</th>
				<th>% Hoàn Thành </th>
				<?php endforeach;?>
				</tr>
				<?php
					$i=0; 
					 foreach ($chitieungaybhlds as $chitieungay):
					  $i++;
				?>
				<tr>
				<td><?php echo $i?></td>
				<td><?php echo $chitieungay['Nhanvien']['Donvi']['madv']?></td>
				<td><?php echo $chitieungay['Nhanvien']['tennv']?></td>
				<td><?php echo $chitieungay['Kehoachbhld']['tendoi']?></td>
				<td><?php echo $chitieungay['Kehoachbhld']['diadiem']?></td>
				<td><?php echo date('d-m-Y',strtotime($chitieungay['Kehoachbhld']['thoigian']))?></td>
				<td><?php echo $chitieungay['Kehoachbhld']['nhansu']?></td>
				<?php foreach ($hanghoabhlds as $keyhh =>$valhh):?>
				<td>
				<?php
					$cosl = 0;
					$cokq = 0;
					$pt = 0;
					foreach ($chitieungay['Chitieungay'] as $ctngay)
					{
						if($ctngay['hanghoa_id']==$keyhh)
						{
				 			echo $ctngay['soluong'];
				 			$tongdk[$keyhh] +=$ctngay['soluong'];
				 			$cosl = 1;
				 			break;
						}	
					}
					if($cosl==0)
						echo 0;
				?>
				</td>
				<td>
				<?php
					foreach ($chitieungay['Chitieungay'] as $ctngay)
					{
						if($ctngay['hanghoa_id']==$keyhh)
						{
				 			echo $ctngay['ketqua'];
				 			$tongkq[$keyhh] +=$ctngay['ketqua'];
				 			$cokq=1;
				 			break;
						}	
					}
					if($cokq==0)
						echo 0;
				?>
				</td>
				<td>
				<?php
					foreach ($chitieungay['Chitieungay'] as $ctngay)
					{
						if($ctngay['hanghoa_id']==$keyhh)
						{
							$num=(empty($ctngay['soluong'])? 0 :$ctngay['ketqua']*100/$ctngay['soluong']);
				 			echo number_format(round($num,2),0,',','.').'%';
				 			$pt = 1;
				 			break;
						}	
					}
					if($pt==0)
						echo "0%";
				?>
				</td>
				<?php endforeach;?>
				<td><?php echo $chitieungay['Kehoachbhld']['ghichu']?></td>
				</tr>
				<?php 
                endforeach;?> 
                <tr ><td colspan=7 style="text-align:center;"> <b>Tổng</b> </td>
                <?php
					foreach ($hanghoabhlds as $keyh=>$valh)
					{?>
				 		<td><?php echo $tongdk[$keyh];?></td>	
				 		<td><?php echo $tongkq[$keyh];?></td>
				 		<td><?php echo number_format((round($tongdk[$keyh]==0?0:$tongkq[$keyh]*100/$tongdk[$keyh],2)),0,',','.').'%';?></td>
					<?php }?>
					<td><?php echo $chitieungay['Kehoachbhld']['ghichu'] ?></td>
                </tr>
				</table>
				<!--<input  type="hidden" name="data[Kqbanhang] [nhanvien] []" value="<?php echo $chitieungay ['Nhanvien'] ['id']?>"/>
--><?php
//	                	echo $form->end('Xuất Excel');

?>
					<p>
					<?php
					echo $this->Paginator->counter(array(
					'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)
					));
					?>	</p>
				
					<div class="paging">
						<?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
					 | 	<?php echo $this->Paginator->numbers();?>
				 |
						<?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
					</div>
					<?php }
					else
					{
					?>
					<h3>Không có dữ liệu</h3>
					<?php }?>